/* PatientSuchenTableModel.java
 *
 * @author Medisoft (Team D)
 * @date 15.04.2008
 *
 * Patient Suchen Table Model Klasse
 */
package ocumed.teams.teamd.application;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

import ocumed.teams.IMedikament;
import ocumed.teams.OcumedFactory;

/**
 * Ocumed
 * 
 * @author Medisoft (Team D)
 * @version 1.0
 * @see Object
 */
/**
 * 
 * 
 */
public class MedikamentSuchenTableModel extends DefaultTableModel {

    private static final long serialVersionUID = 1L;
    private Map<Integer, String> m_filter = new HashMap<Integer, String>();
    private List<IMedikament> m_data;
    private List<IMedikament> m_modeldata;

    public MedikamentSuchenTableModel() {
        super();

        try {

            // �berschriften der JTable
            Vector<String> vec = new Vector<String>();

            vec.add("Name");
            vec.add("Packungsgroesse");
            vec.add("Name#");

            setColumnCount(vec.size() - 1);
            setColumnIdentifiers(vec);

            m_data = OcumedFactory.getInstance().getiMedikamente();
            m_modeldata = m_data;
            
            Collections.sort(m_data, new ModelComparator());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public int getId(int row) {
        IMedikament p = m_modeldata.get(row);

        return p.getMedikamentid();
    }

    @Override
    public Object getValueAt(int row, int col) {
        if (m_modeldata != null) {
            IMedikament p = m_modeldata.get(row);
            if (p != null) {
                switch (col) {
                    case 0:
                        return p.getMedikamentname();
                    case 1:
                        return p.getMedikamentpackungsgroesse();

                }
            }
        }
        return null;
    }

    @Override
    public int getRowCount() {
        if (m_modeldata != null) {
            return m_modeldata.size();
        } else {
            return 0;
        }
    }

    private boolean comp(IMedikament p, Map<Integer, String> filters) {
        String value = null;
        Boolean ret = true;

        Iterator<Map.Entry<Integer, String>> it = filters.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, String> pairs = (Map.Entry<Integer, String>) it.next();

            Integer col = (Integer) pairs.getKey();
            String val = (String) pairs.getValue();

            switch (col) {
                case 0:
                    value = p.getMedikamentname();
                    break;
                case 1:
                    value = p.getMedikamentpackungsgroesse();
                    break;
            }

            if (value == null) {
                value = "";
            }

            if (!value.toUpperCase().startsWith(val)) {
                ret = false;
            }

        }

        return ret;
    }

    public void setFilter(String filter, int col) {
        if (m_filter.containsKey(col)) {
            m_filter.remove(col);
        }
        m_filter.put(col, filter);

        List<IMedikament> lp = new ArrayList<IMedikament>();

        for (IMedikament p : m_data) {
            if (comp(p, m_filter)) {
                lp.add(p);
            }

        }

        m_modeldata = lp;

    }
    
    private class ModelComparator implements Comparator {

        @Override
        public int compare(Object op1, Object op2) {
            int result = 0;

            IMedikament m1 = (IMedikament) op1;
            IMedikament m2 = (IMedikament) op2;

            result = m1.getMedikamentname().compareTo(m2.getMedikamentname());
            
            return result;
        }
    }
}
